using System;
using System.Collections.Generic;

using Blog.Net.Common.DataObjects;
using Blog.Net.Business.BusinessObjects;

using NHibernate;
using NHibernate.Criterion;
using CureConsulting.DataAccess;

namespace Blog.Net.DataAccess.DataControls
{
	public partial class RoleDataControl : IDataControl<RoleBO>
	{
		#region FieldNames
		
		/// <summary>
        /// Struct to hold field names
        /// </summary>
		public struct FieldNames
        {
			/// <summary>
        	/// HQL Property Name for Id
        	/// </summary>
			public const string Id = "Id";
			/// <summary>
        	/// HQL Property Name for Name
        	/// </summary>
			public const string Name = "Name";
			/// <summary>
        	/// HQL Property Name for Description
        	/// </summary>
			public const string Description = "Description";
			/// <summary>
        	/// HQL Property Name for Logins
        	/// </summary>
			public const string Logins = "Logins";
		}
		
		#endregion
		
		#region Private Members
		
		private static RoleDataControl roleDataControl;
		private static object lockRoleDataControl = new object();
		private static NHibernateDataControl<Role> dataControl = new NHibernateDataControl<Role>();
		
		#endregion
		
		#region Constructors
		
		/// <summary>
		/// Constructor for role.
		/// </summary>
		public RoleDataControl(){}
		
		#endregion
		
		#region Private Methods

        private static IList<RoleBO> ConvertPOCOListToBOList(IList<Role> roles)
        {
            List<RoleBO> retVal = new List<RoleBO>();

            if (roles != null)
            {
                foreach (Role role in roles)
                {
                    retVal.Add(new RoleBO(role));
                }
            }

            return retVal;
        }

        #endregion
		
		#region Public Properties

        /// <summary>
        ///Singleton Instance
        /// </summary>
        public static RoleDataControl Instance
        {
            get
            {
                lock (lockRoleDataControl)
                {
                    if(roleDataControl == null)
                    {
                        roleDataControl = new RoleDataControl();
                    }
                }

                return roleDataControl;
            }
        }

        #endregion
		
		#region CRUD

        /// <summary>
        /// Function to Save a Role
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public int? Save(RoleBO role)
        {
            int? retVal = dataControl.Save(role.POCO);
            if (retVal.HasValue)
            	dataControl.Refresh(role.POCO);
            return retVal;
        }

        /// <summary>
        /// Function to Save a list of roles
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        public bool Save(IList<RoleBO> roles)
        {
            List<Role> itemsToSave = new List<Role>();

            foreach (RoleBO role in roles)
            {
                itemsToSave.Add(role.POCO);
            }

            return dataControl.Save(itemsToSave);
        }

        /// <summary>
        /// Function to delete a Role
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool Delete(RoleBO role)
        {
            return dataControl.Delete(role.POCO);
        }

        /// <summary>
        /// Function to Delete a list of roles
        /// </summary>
        /// <param name="roles"></param>
        /// <returns></returns>
        public bool Delete(IList<RoleBO> roles)
        {
            List<Role> itemsToDelete = new List<Role>();

            foreach (RoleBO role in roles)
            {
                itemsToDelete.Add(role.POCO);
            }

            return dataControl.Delete(itemsToDelete);
        }		
		
        #endregion
		
		#region Queries
		
		/// <summary>
        /// Get Role by ID
        /// </summary>
        /// <param name="id">ID of Role</param>
        /// <returns>The Role</returns>
        public RoleBO GetById(int? id)
        {
			Role retVal = dataControl.GetObjectById(id);
			
			if(retVal != null)
				return new RoleBO(retVal);
			
			return null;
		}
		
		/// <summary>
        /// Return list of all roles
        /// </summary>
        /// <returns></returns>
        public IList<RoleBO> GetAll()
        {
			IList<Role> retVal = dataControl.GetQueryResult(dataControl.GetCriteria());

            if (retVal != null)
                return ConvertPOCOListToBOList(retVal);
         
			return new List<RoleBO>();
        }
		
		/// <summary>
        /// Return list of all roles
        /// </summary>
        /// <param name="firstResult"></param>
        /// <param name="maxResults"></param>
        /// <returns></returns>
        public IList<RoleBO> GetAll(int firstResult, int maxResults)
        {
			IList<Role> retVal = dataControl.GetQueryResult(dataControl.GetCriteria(), firstResult, maxResults);

            if (retVal != null)
                return ConvertPOCOListToBOList(retVal);
            
			return new List<RoleBO>();
        }
		
		/// <summary>
        /// Return list of all roles
        /// </summary>
        /// <param name="firstResult"></param>
        /// <param name="maxResults"></param>
		/// <param name="sortExpression"></param>
        /// <returns></returns>
        public IList<RoleBO> GetAll(int firstResult, int maxResults, string sortExpression)
        {
			ICriteria criteria = dataControl.GetCriteria();

            if (!String.IsNullOrEmpty(sortExpression))
            {
                string[] sort = sortExpression.Split(' ');
                bool ascending = true;
                if (sort.Length > 1 && sort[1].ToUpper() == "DESC") { ascending = false; }
                criteria.AddOrder(new NHibernate.Criterion.Order(sort[0],ascending));
            }
			
			IList<Role> retVal = dataControl.GetQueryResult(criteria, firstResult, maxResults);

            if (retVal != null)
                return ConvertPOCOListToBOList(retVal);
        
			return new List<RoleBO>();
        }
		
		/// <summary>
        /// Return count of all roles
        /// </summary>
        /// <returns></returns>
        public int GetCountOfAll()
        {			
			int result = (int)dataControl.GetCriteria().SetProjection(Projections.RowCount()).UniqueResult();
			return result;
        }
		
		/// <summary>
        /// Query to get roles by Name
        /// </summary>
		/// <param name="name">Name of the Role</param>
		/// <param name="firstResult"></param>
        /// <returns>The roles</returns>
        public RoleBO GetByName(string name)
        {
			ICriteria criteria = dataControl.GetCriteria();
            criteria.Add(Expression.Eq(FieldNames.Name, name));
						
			Role retVal = dataControl.GetUniqueQueryResult(criteria);
			
			if(retVal != null)
				return new RoleBO(retVal);
				
			return null;	
        }
		
		#endregion
	}
}